//
//  CrudeOilCode.m
//  DataCollector
//
//  Created by jiro on 12/06/11.
//  Copyright 2012 jiro music. All rights reserved.
//

#import "CrudeOilCode.h"

#import "../DbConstants.h"
#import "CrudeOilProductCountryCode.h"

@implementation CrudeOilCode

@synthesize productCountryCode;
+ (id) crudeOilCodeWithValues: (NSArray*) values
{
	CrudeOilCode* ret = [ [ CrudeOilCode alloc ] init ];
	ret.code = [ values objectAtIndex: 0 ];
	ret.name = [ values objectAtIndex: 1 ];
	return ret;
	
}

+ (NSString*) tableName
{
	return kText_CrudeOilCodeTable;
}

+ (NSString*) queryCreateTable
{
	NSString* s = [ NSString stringWithFormat: @"create table %@( %@ %@, %@ %@, %@ %@, %@( %@, %@ ) );",
		kText_CrudeOilCodeTable, 
		kText_Code, kText_Text,
		kText_Name, kText_Name,
		kText_CrudeOilProductCountryCode, kText_Text,
		kText_PrimaryKey, kText_Code, kText_CrudeOilProductCountryCode ];
	return s;
}

- (NSString*) sqlInsertStatement
{
	NSString* s = [ NSString stringWithFormat: @"insert into %@ values %@;", kText_CrudeOilCodeTable, [ self queryInsertValue ] ];
	return s;
}

+ (NSString*) queryInsertInitialValues
{

	struct CrudeOilCodeStruct { CrudeOilCodeType::Type code; NSString* name; CrudeOilProductCountryCodeType::Type productCountryCode; };
	CrudeOilCodeStruct codes[] = {
		{ CrudeOilCodeType::NoName, @"", CrudeOilProductCountryCodeType::TotalWorld },
		{ CrudeOilCodeType::NoName, @"", CrudeOilProductCountryCodeType::UnitedStates },
		{ CrudeOilCodeType::NoName, @"", CrudeOilProductCountryCodeType::OPECAverage },
		{ CrudeOilCodeType::Murban39, @"Murban39", CrudeOilProductCountryCodeType::AbuDhabi },
		{ CrudeOilCodeType::SaharanBlend44, @"SaharanBlend44", CrudeOilProductCountryCodeType::Algeria },
		{ CrudeOilCodeType::Cabinda32, @"Cabinda32", CrudeOilProductCountryCodeType::Angola },
		{ CrudeOilCodeType::Fateh32, @"Fateh32", CrudeOilProductCountryCodeType::Dubai },
		{ CrudeOilCodeType::Oriente30, @"Oriente30", CrudeOilProductCountryCodeType::Ecuador },
		{ CrudeOilCodeType::Heavy30, @"Heavy30", CrudeOilProductCountryCodeType::Iran },
		{ CrudeOilCodeType::Light34, @"Light34", CrudeOilProductCountryCodeType::Iran },
		{ CrudeOilCodeType::Kirkuk36, @"Kirkuk36", CrudeOilProductCountryCodeType::Iraq },
		{ CrudeOilCodeType::Kuwait31, @"Kuwait31", CrudeOilProductCountryCodeType::Kuwait },
		{ CrudeOilCodeType::EsSider37, @"EsSider37", CrudeOilProductCountryCodeType::Libya },
		{ CrudeOilCodeType::Khafji28, @"Khafji28", CrudeOilProductCountryCodeType::NeutralZone },
		{ CrudeOilCodeType::BonnyLight37, @"BonnyLight37", CrudeOilProductCountryCodeType::Nigeria },
		{ CrudeOilCodeType::Forcados31, @"Forcados31", CrudeOilProductCountryCodeType::Nigeria },
		{ CrudeOilCodeType::Dukhan40, @"Dukhan40", CrudeOilProductCountryCodeType::Qatar },
		{ CrudeOilCodeType::ArabianHeavy27, @"ArabianHeavy27", CrudeOilProductCountryCodeType::SaudiArabia },
		{ CrudeOilCodeType::ArabianLight34, @"ArabianLight34", CrudeOilProductCountryCodeType::SaudiArabia },
		{ CrudeOilCodeType::ArabianMedium31, @"ArabianMedium31", CrudeOilProductCountryCodeType::SaudiArabia },
		{ CrudeOilCodeType::Bachaquero17, @"Bachaquero17", CrudeOilProductCountryCodeType::Venezuela },
		{ CrudeOilCodeType::Bachaquero24, @"Bachaquero24", CrudeOilProductCountryCodeType::Venezuela },
		{ CrudeOilCodeType::TiaJuanaLight31, @"TiaJuanaLight31", CrudeOilProductCountryCodeType::Venezuela },
		{ CrudeOilCodeType::Gippsland42, @"Gippsland42", CrudeOilProductCountryCodeType::Australia },
		{ CrudeOilCodeType::SeriaLight37, @"SeriaLight37", CrudeOilProductCountryCodeType::Brunei },
		{ CrudeOilCodeType::Kole34, @"Kole34", CrudeOilProductCountryCodeType::Cameroon },
		{ CrudeOilCodeType::CanadianPar40, @"CanadianPar40", CrudeOilProductCountryCodeType::Canada },
		{ CrudeOilCodeType::HeavyHardisty22, @"HeavyHardisty22", CrudeOilProductCountryCodeType::Canada },
		{ CrudeOilCodeType::Lloydblend22, @"Lloydblend22", CrudeOilProductCountryCodeType::Canada },
		{ CrudeOilCodeType::Daqing33, @"Daqing33", CrudeOilProductCountryCodeType::China },
		{ CrudeOilCodeType::CanoLimon30, @"CanoLimon30", CrudeOilProductCountryCodeType::Colombia },
		{ CrudeOilCodeType::SuezBlend33, @"SuezBlend33", CrudeOilProductCountryCodeType::Egypt },
		{ CrudeOilCodeType::Mandji30, @"Mandji30", CrudeOilProductCountryCodeType::Gabon },
		{ CrudeOilCodeType::Minas34, @"Minas34", CrudeOilProductCountryCodeType::Indonesia },
		{ CrudeOilCodeType::TapisBlend44, @"TapisBlend44", CrudeOilProductCountryCodeType::Malaysia },
		{ CrudeOilCodeType::Isthmus33, @"Isthmus33", CrudeOilProductCountryCodeType::Mexico },
		{ CrudeOilCodeType::Maya22, @"Maya22", CrudeOilProductCountryCodeType::Mexico },
		{ CrudeOilCodeType::EkofiskBlend42, @"EkofiskBlend42", CrudeOilProductCountryCodeType::Norway },
		{ CrudeOilCodeType::OmanBlend34, @"OmanBlend34", CrudeOilProductCountryCodeType::Oman },
		{ CrudeOilCodeType::Urals32, @"Urals32", CrudeOilProductCountryCodeType::Russia },
		{ CrudeOilCodeType::BrentBlend38, @"BrentBlend38", CrudeOilProductCountryCodeType::UnitedKingdom },
		{ CrudeOilCodeType::NoName, @"", CrudeOilProductCountryCodeType::NonOPECAverage },
	};

	NSString* s = [ NSString string ];
	for ( int i = 0, count = sizeof( codes ) / sizeof( CrudeOilCodeStruct ); i < count; i++ ) 
	{
		CrudeOilCode* code = [ [ CrudeOilCode alloc ] init ];
		code.productCountryCode = [ NSString stringWithFormat: @"%d", codes[ i ].productCountryCode ];
		code.code = [ NSString stringWithFormat: @"%d", codes[ i ].code ];
		code.name = codes[ i ].name;
		
		s = [ s stringByAppendingString:[ code queryInsertValue ] ];
		if ( i != count - 1 )
		{
			s = [ s stringByAppendingString: @", " ];
		}
	}
	return s;
}

+ (NSString*) querySelectAll
{
	NSString* s = [ NSString stringWithFormat: @"select * from %@;", [ CrudeOilCode tableName ] ];
	return s;
}

- (NSString*) queryInsert
{
	NSString* s = [ NSString stringWithFormat: @"insert into * values %@;", 
		[ CrudeOilCode tableName ], [ self queryInsertValue ] ];
	return s;
}

- (NSString*) queryInsertValue
{
	NSString* s = [ NSString stringWithFormat: @"( '%@', '%@', '%@' )", code, name, productCountryCode ];
	return s;
}

@end
